package com.hwh.blog.web;

import com.hwh.blog.handler.NotFoundException;
import com.hwh.blog.po.Type;
import com.hwh.blog.qu.BlogQuery;
import com.hwh.blog.service.BlogServiceImpl;
import com.hwh.blog.service.TagServiceImpl;
import com.hwh.blog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {
    @Autowired
    private BlogServiceImpl blogservice;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagServiceImpl tagService;
    @RequestMapping("/blog")
    public String Blog(){

        return "/blog";
    }
    @GetMapping("/")
    public String GotoIndex(@PageableDefault(size = 5) Pageable pageable, Model model){
        model.addAttribute("page",blogservice.listBlog(pageable));
        model.addAttribute("types",typeService.listType(10));
        model.addAttribute("tags",tagService.listTagTop(6));
        model.addAttribute("recommendBlogs",blogservice.listRecommendBlogTop(8));
        return "index";
    }
    @RequestMapping("/search")
    public String search(@PageableDefault(size = 5) Pageable pageable,String query, Model model){
        model.addAttribute("page",blogservice.listBlog(pageable,"%"+query+"%"));
        model.addAttribute("query",query);
        return "search";
    }
    @GetMapping("/blog/{id}")
    public String blog(@PathVariable Long id,Model model){
        model.addAttribute("blog",blogservice.getAndConvert(id));
        return "blog";
    }
    @GetMapping("/about")
    public String about(){
        return "about";
    }
}
